import preferences from "@ohos.data.preferences"


class PreferencesUtil {
  TAG: string = "PreferencesUtil"
  perfMap: Map<string, preferences.Preferences> = new Map()
  User: string = "User"
  Content: string = "Content"

  async loadPreferences(context, name: string) {
    try {
      let data = await preferences.getPreferences(context, name)
      this.perfMap.set(name, data)
      console.error(this.TAG, `加载PreferencesUtil[${name}]成功`)
    } catch (e) {
      console.error(this.TAG, `加载PreferencesUtil[${name}]失败`, JSON.stringify(e))
    }
  }
  //增加用户信息
  async putSPreferencesValue(type: string, key: string, value: preferences.ValueType) {
    if (!this.perfMap.has(type)) {
      console.error(this.TAG, `PreferencesUtil[${this.User}]未初始化`)
      return
    }
    try {
      let pref = this.perfMap.get(type)
      //写入数据
      await pref.put(key, value)
      await pref.flush()
      console.error(this.TAG, `保存PreferencesUtil[${type}.${key}==${value}]成功`)
    } catch (e) {
      console.error(this.TAG, `保存PreferencesUtil[${type}.${key}==${value}]失败`)
    }
  }
  //增加用户信息
  async putUserPreferencesValue(key: string, value: preferences.ValueType) {
    if (!this.perfMap.has(this.User)) {
      console.error(this.TAG, `PreferencesUtil[${this.User}]未初始化`)
      return
    }
    try {
      let pref = this.perfMap.get(this.User)
      //写入数据
      await pref.put(key, value)
      await pref.flush()
      console.error(this.TAG, `保存PreferencesUtil[${this.User}.${key}==${value}]成功`)
    } catch (e) {
      console.error(this.TAG, `保存PreferencesUtil[${this.User}.${key}==${value}]失败`)
    }
  }
  //增加内容信息
  async putPreferencesValue(key: string, value: preferences.ValueType) {
    if (!this.perfMap.has(this.Content)) {
      console.error(this.TAG, `PreferencesUtil[${this.Content}]未初始化`)
      return
    }
    try {
      let pref = this.perfMap.get(this.Content)
      //写入数据
      await pref.put(key, value)
      await pref.flush()
      console.error(this.TAG, `保存PreferencesUtil[${this.Content}.${key}==${value}]成功`)
    } catch (e) {
      console.error(this.TAG, `保存PreferencesUtil[${this.Content}.${key}==${value}]失败`)
    }
  }
  //读取数据内容信息
  async getSPreferencesValue(type: string, key: string, defaultValue: preferences.ValueType) {
    if (!this.perfMap.has(type)) {
      console.error(this.TAG, `读取PreferencesUtil[${type}]未初始化`)
      return defaultValue
    }
    try {
      //读取数据
      const value = await this.perfMap.get(type).get(key, defaultValue)
      console.error(this.TAG, `读取PreferencesUtil[${type}.${key}==${value}]成功`)
      return value
    } catch (e) {
      return defaultValue
      console.error(this.TAG, `读取PreferencesUtil[${type}.${key}}]失败`, JSON.stringify(e))
    }
  }
  //读取数据内容信息
  async getPreferencesValue(key: string, defaultValue: preferences.ValueType) {
    if (!this.perfMap.has(this.Content)) {
      console.error(this.TAG, `读取PreferencesUtil[${this.Content}]未初始化`)
      return defaultValue
    }
    try {
      //读取数据
      const value = await this.perfMap.get(this.Content).get(key, defaultValue)
      console.error(this.TAG, `读取PreferencesUtil[${this.Content}.${key}==${value}]成功`)
      return value
    } catch (e) {
      return defaultValue
      console.error(this.TAG, `读取PreferencesUtil[${this.Content}.${key}}]失败`, JSON.stringify(e))
    }
  }
  //读取用户信息
  async getUserPreferencesValue(key: string, defaultValue: preferences.ValueType) {
    if (!this.perfMap.has(this.User)) {
      console.error(this.TAG, `读取PreferencesUtil[${this.User}]未初始化`)
      return defaultValue
    }
    try {
      //读取数据
      const value = await this.perfMap.get(this.User).get(key, defaultValue)
      console.error(this.TAG, `读取PreferencesUtil[${this.User}.${key}==${value}]成功`)
      return value
    } catch (e) {
      return defaultValue
      console.error(this.TAG, `读取PreferencesUtil[${this.User}.${key}}]失败`, JSON.stringify(e))
    }
  }
}

const preferencesUtil = new PreferencesUtil();

export default preferencesUtil as PreferencesUtil
